/**
 * Created by Hon on 2017/7/14.
 */
$(document).ready(function () {
    //localStorage.clear();
    var flag=false;
    for (var i=0;i<localStorage.length;i++){  //调用key方法获取localStorage中数据对应的键名
        //localStorage.key(0)对应data1
        var getKey=localStorage.key(i);
        var str=localStorage.getItem(getKey);
        var data=JSON.parse(str);
        if (data.stat===1){
                flag =true;
                var welMsg="欢迎您，";
                $("#user").append('<span class="weui-check__label">'+welMsg+data.username+'</span>');
                for (var j=0;j<localStorage.length;j++){
                    var Key=localStorage.key(j);
                    var str2=localStorage.getItem(Key);
                    var datashow=JSON.parse(str2);
                    $("#infoshow").append('<div class="weui-cell"><div class="weui-cell__bd"><p>'+datashow.username+'</p></div><div class="weui-cell__ft"><p>'+datashow.mail+'</p></div></div>');
                }
            }
    }
    if (!flag) {    //说明是直接打开
        $.toast("您没有登录",function () {
            console.log('close');
            window.location.href="LogIn.html";    //跳转到登录页面
        });
    }
    $("#logout").click(function () {    //注销时把当前用户的登录状态设为0
        for (var i=0;i<localStorage.length;i++){

            var getKey=localStorage.key(i);
            var str=localStorage.getItem(getKey);
            var data=JSON.parse(str);
            if (data.stat===1){
                data.stat=0;
                data.str= JSON.stringify(data);
                localStorage.setItem("userID"+data.username,data.str);
            } //注销当前用户的状态
        }
        $.toast("注销成功",function () {
            console.log('close');
            window.location.href="LogIn.html";    //跳转到登录页面
        });
    });

    $(window).bind('beforeunload',function(){        //即将关闭页面
        //响应事件
        $("#logout").trigger('click');
       //关闭页面时也进行登录状态的修改
    });
});
